#include <iostream>
#include <unistd.h>
#include <cstring>
#include <arpa/inet.h>
#include "Sock.hpp"

using namespace std;

const int BUF_SIZE = 1024;

int main(int argc, char* argv[]) {
    if (argc != 3) {
        cerr << "Usage: " << argv[0] << " IP PORT" << endl;
        return 1;
    }

    string serverIp = argv[1];
    uint16_t serverPort = stoi(argv[2]);

    int clientSock = Sock::Socket();
    if (!Sock::Connect(clientSock, serverIp, serverPort)) {
        cerr << "Failed to connect to server" << endl;
        return 1;
    }

    cout << "input message to server: " << endl;
    string message;
    std::getline(cin, message);
    send(clientSock, message.c_str(), message.length(), 0);

    char buffer[BUF_SIZE];
    ssize_t len = recv(clientSock, buffer, BUF_SIZE - 1, 0);
    if (len > 0) {
        buffer[len] = '\0';
        cout << "Received from server: " << buffer << endl;
    } else {
        cerr << "Recv error or server closed connection" << endl;
    }

    close(clientSock);
    return 0;
}
